﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace suirui.ZhuMu.Common
{
    public class Util
    {
        private static char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

        public static long Next(long max)
        {
            rngp.GetBytes(rb);
            long value = BitConverter.ToInt64(rb, 0);
            value = value % (max + 1);
            return Math.Abs(value);
        }
        private static RNGCryptoServiceProvider rngp = new RNGCryptoServiceProvider();
        /// <summary>
        /// 邮件格式正则表达式
        /// </summary>
        private static Regex _emailregex = new Regex(@"^((?'name'.+?)\s*<)?(?'email'(?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""(?'user'(?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?'angle'<))?(?'user'(?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")@(?'domain'((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?'angle')(?(name)>)$", RegexOptions.Multiline | RegexOptions.ExplicitCapture);
        /// <summary>
        /// 电话号码正则表达式
        /// </summary>
        private static Regex _mobileregex = new Regex(@"^1[34578]\d{9}$");
        /// <summary>
        /// 身份证号码格式正则表达式
        /// </summary>
        private static Regex _idCardregex = new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");

        private static byte[] rb = new byte[16];
        private static Regex _weixinregex = new Regex(@"MicroMessenger", RegexOptions.IgnoreCase);

        private static readonly Regex RegexMobile =
        new Regex(@"(iemobile|iphone|ipod|android|nokia|sonyericsson|blackberry|samsung|sec\-|windows ce|motorola|mot\-|up.b|midp\-)",
        RegexOptions.IgnoreCase | RegexOptions.Compiled);


        public static long GetRandNum(int numCount)
        {
            string minstr = "1";
            string maxstr = "9";
            minstr = minstr.PadRight(numCount, '0');
            maxstr = maxstr.PadRight(numCount, '9');

            long min = long.Parse(minstr);
            long max = long.Parse(maxstr);
            return Next(min, max);
        }
        public static string RandomString(int size, bool lowerCase)
        {
            string str = RandomString(size);
            if (!lowerCase)
            {
                return str.ToUpper();
            }
            return str.ToLower();
        }
        public static string RandomString(int strLength)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < strLength; i++)
            {
                var rnd = Next(chars.Length - 1);
                sb.Append(chars[rnd]);
            }
            return sb.ToString();
        }
        public static long Next(long min, long max)
        {
            long value = Next(max - min) + min;
            return value;
        }

        /// <summary>
        /// 是否为邮箱名
        /// </summary>
        public static bool IsEmail(string s)
        {
            if (string.IsNullOrEmpty(s))
                return false;
            return _emailregex.IsMatch(s);
        }

        /// <summary>
        /// 是否为手机号
        /// </summary>
        public static bool IsMobile(string s)
        {
            if (string.IsNullOrEmpty(s))
                return false;
            return _mobileregex.IsMatch(s);
        }
        /// <summary>
        /// 是否为身份证号码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsIdCard(string s)
        {
            if (string.IsNullOrEmpty(s))
                return false;
            return _idCardregex.IsMatch(s);
        }

        private static string[] telStarts = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153,180,181,182,183,185,186,176,187,188,189,177,178".Split(',');
        private static Random ran = new Random();
        /// <summary>
        /// 随机生成电话号码
        /// </summary>
        /// <returns></returns>
        public static string getRandomTel()
        {
            int n = ran.Next(10, 1000);
            int index = ran.Next(0, telStarts.Length - 1);
            string first = telStarts[index];
            string second = (ran.Next(100, 888) + 10000).ToString().Substring(1);
            string thrid = (ran.Next(1, 9100) + 10000).ToString().Substring(1);
            return first + second + thrid;
        }
    }

}